home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cool / cool.lha / ice / pisces / flex / sym.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-04  |  7.1 KB  |  315 lines

  1. /* sym - symbol table routines */
  2.  
  3. /*
  4.  * Copyright (c) 1989 The Regents of the University of California.
  5.  * All rights reserved.
  6.  *
  7.  * This code is derived from software contributed to Berkeley by
  8.  * Vern Paxson.
  9.  * 
  10.  * The United States Government has rights in this work pursuant to
  11.  * contract no. DE-AC03-76SF00098 between the United States Department of
  12.  * Energy and the University of California.
  13.  *
  14.  * Redistribution and use in source and binary forms are permitted
  15.  * provided that the above copyright notice and this paragraph are
  16.  * duplicated in all such forms and that any documentation,
  17.  * advertising materials, and other materials related to such
  18.  * distribution and use acknowledge that the software was developed
  19.  * by the University of California, Berkeley.  The name of the
  20.  * University may not be used to endorse or promote products derived
  21.  * from this software without specific prior written permission.
  22.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  23.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  24.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  25.  */
  26.  
  27. #ifndef lint
  28.  
  29. static char copyright[] =
  30.     "@(#) Copyright (c) 1989 The Regents of the University of California.\n";
  31. static char CR_continuation[] = "@(#) All rights reserved.\n";
  32.  
  33. static char rcsid[] =
  34.     "@(#) $Header: /tan/u1/neath/pisces/flex/RCS/sym.c,v 1.1 90/05/15 13:14:11 neath Exp $ (LBL)";
  35.  
  36. #endif
  37.  
  38. #include "flexdef.h"
  39.  
  40. struct hash_entry *ndtbl[NAME_TABLE_HASH_SIZE];
  41. struct hash_entry *sctbl[START_COND_HASH_SIZE];
  42. struct hash_entry *ccltab[CCL_HASH_SIZE];
  43.  
  44. struct hash_entry *findsym();
  45.  
  46.  
  47. /* addsym - add symbol and definitions to symbol table
  48.  *
  49.  * synopsis
  50.  *    char sym[], *str_def;
  51.  *    int int_def;
  52.  *    hash_table table;
  53.  *    int table_size;
  54.  *    0 / -1 = addsym( sym, def, int_def, table, table_size );
  55.  *
  56.  * -1 is returned if the symbol already exists, and the change not made.
  57.  */
  58.  
  59. int addsym( sym, str_def, int_def, table, table_size )
  60. register char sym[];
  61. char *str_def;
  62. int int_def;
  63. hash_table table;
  64. int table_size;
  65.  
  66.     {
  67.     int hash_val = hashfunct( sym, table_size );
  68.     register struct hash_entry *sym_entry = table[hash_val];
  69.     register struct hash_entry *new_entry;
  70.     register struct hash_entry *successor;
  71.     char *malloc();
  72.  
  73.     while ( sym_entry )
  74.     {
  75.     if ( ! strcmp( sym, sym_entry->name ) )
  76.         { /* entry already exists */
  77.         return ( -1 );
  78.         }
  79.     
  80.     sym_entry = sym_entry->next;
  81.     }
  82.  
  83.     /* create new entry */
  84.     new_entry = (struct hash_entry *) malloc( sizeof( struct hash_entry ) );
  85.  
  86.     if ( new_entry == NULL )
  87.     flexfatal( "symbol table memory allocation failed" );
  88.  
  89.     if ( (successor = table[hash_val]) )
  90.     {
  91.     new_entry->next = successor;
  92.     successor->prev = new_entry;
  93.     }
  94.     else
  95.     new_entry->next = NULL;
  96.  
  97.     new_entry->prev = NULL;
  98.     new_entry->name = sym;
  99.     new_entry->str_val = str_def;
  100.     new_entry->int_val = int_def;
  101.  
  102.     table[hash_val] = new_entry;
  103.  
  104.     return ( 0 );
  105.     }
  106.  
  107.  
  108. /* cclinstal - save the text of a character class
  109.  *
  110.  * synopsis
  111.  *    char ccltxt[];
  112.  *    int cclnum;
  113.  *    cclinstal( ccltxt, cclnum );
  114.  */
  115.  
  116. cclinstal( ccltxt, cclnum )
  117. char ccltxt[];
  118. int cclnum;
  119.  
  120.     {
  121.     /* we don't bother checking the return status because we are not called
  122.      * unless the symbol is new
  123.      */
  124.     char *copy_string();
  125.  
  126.     (void) addsym( copy_string( ccltxt ), (char *) 0, cclnum,
  127.            ccltab, CCL_HASH_SIZE );
  128.     }
  129.  
  130.  
  131. /* ccllookup - lookup the number associated with character class text
  132.  *
  133.  * synopsis
  134.  *    char ccltxt[];
  135.  *    int ccllookup, cclval;
  136.  *    cclval/0 = ccllookup( ccltxt );
  137.  */
  138.  
  139. int ccllookup( ccltxt )
  140. char ccltxt[];
  141.  
  142.     {
  143.     return ( findsym( ccltxt, ccltab, CCL_HASH_SIZE )->int_val );
  144.     }
  145.  
  146.  
  147. /* findsym - find symbol in symbol table
  148.  *
  149.  * synopsis
  150.  *    char sym[];
  151.  *    hash_table table;
  152.  *    int table_size;
  153.  *    struct hash_entry *sym_entry, *findsym();
  154.  *    sym_entry = findsym( sym, table, table_size );
  155.  */
  156.  
  157. struct hash_entry *findsym( sym, table, table_size )
  158. register char sym[];
  159. hash_table table;
  160. int table_size;
  161.  
  162.     {
  163.     register struct hash_entry *sym_entry = table[hashfunct( sym, table_size )];
  164.     static struct hash_entry empty_entry =
  165.     {
  166.     (struct hash_entry *) 0, (struct hash_entry *) 0, NULL, NULL, 0,
  167.     } ;
  168.  
  169.     while ( sym_entry )
  170.     {
  171.     if ( ! strcmp( sym, sym_entry->name ) )
  172.         return ( sym_entry );
  173.     sym_entry = sym_entry->next;
  174.     }
  175.  
  176.     return ( &empty_entry );
  177.     }
  178.  
  179.     
  180. /* hashfunct - compute the hash value for "str" and hash size "hash_size"
  181.  *
  182.  * synopsis
  183.  *    char str[];
  184.  *    int hash_size, hash_val;
  185.  *    hash_val = hashfunct( str, hash_size );
  186.  */
  187.  
  188. int hashfunct( str, hash_size )
  189. register char str[];
  190. int hash_size;
  191.  
  192.     {
  193.     register int hashval;
  194.     register int locstr;
  195.  
  196.     hashval = 0;
  197.     locstr = 0;
  198.  
  199.     while ( str[locstr] )
  200.     hashval = ((hashval << 1) + str[locstr++]) % hash_size;
  201.  
  202.     return ( hashval );
  203.     }
  204.  
  205.  
  206. /* ndinstal - install a name definition
  207.  *
  208.  * synopsis
  209.  *    char nd[], def[];
  210.  *    ndinstal( nd, def );
  211.  */
  212.  
  213. ndinstal( nd, def )
  214. char nd[], def[];
  215.  
  216.     {
  217.     char *copy_string();
  218.  
  219.     if ( addsym( copy_string( nd ), copy_string( def ), 0,
  220.          ndtbl, NAME_TABLE_HASH_SIZE ) )
  221.     synerr( "name defined twice" );
  222.     }
  223.  
  224.  
  225. /* ndlookup - lookup a name definition
  226.  *
  227.  * synopsis
  228.  *    char nd[], *def;
  229.  *    char *ndlookup();
  230.  *    def/NULL = ndlookup( nd );
  231.  */
  232.  
  233. char *ndlookup( nd )
  234. char nd[];
  235.  
  236.     {
  237.     return ( findsym( nd, ndtbl, NAME_TABLE_HASH_SIZE )->str_val );
  238.     }
  239.  
  240.  
  241. /* scinstal - make a start condition
  242.  *
  243.  * synopsis
  244.  *    char str[];
  245.  *    int xcluflg;
  246.  *    scinstal( str, xcluflg );
  247.  *
  248.  * NOTE
  249.  *    the start condition is Exclusive if xcluflg is true
  250.  */
  251.  
  252. scinstal( str, xcluflg )
  253. char str[];
  254. int xcluflg;
  255.  
  256.     {
  257.     char *copy_string();
  258.  
  259.     /* bit of a hack.  We know how the default start-condition is
  260.      * declared, and don't put out a define for it, because it
  261.      * would come out as "#define 0 1"
  262.      */
  263.     /* actually, this is no longer the case.  The default start-condition
  264.      * is now called "INITIAL".  But we keep the following for the sake
  265.      * of future robustness.
  266.      */
  267.  
  268.     if ( strcmp( str, "0" ) )
  269.     printf( "#define %s %d\n", str, lastsc );
  270.  
  271.     if ( ++lastsc >= current_max_scs )
  272.     {
  273.     current_max_scs += MAX_SCS_INCREMENT;
  274.  
  275.     ++num_reallocs;
  276.  
  277.     scset = reallocate_integer_array( scset, current_max_scs );
  278.     scbol = reallocate_integer_array( scbol, current_max_scs );
  279.     scxclu = reallocate_integer_array( scxclu, current_max_scs );
  280.     sceof = reallocate_integer_array( sceof, current_max_scs );
  281.     scname = reallocate_char_ptr_array( scname, current_max_scs );
  282.     actvsc = reallocate_integer_array( actvsc, current_max_scs );
  283.     }
  284.  
  285.     scname[lastsc] = copy_string( str );
  286.  
  287.     if ( addsym( scname[lastsc], (char *) 0, lastsc,
  288.          sctbl, START_COND_HASH_SIZE ) )
  289.     lerrsf( "start condition %s declared twice", str );
  290.  
  291.     scset[lastsc] = mkstate( SYM_EPSILON );
  292.     scbol[lastsc] = mkstate( SYM_EPSILON );
  293.     scxclu[lastsc] = xcluflg;
  294.     sceof[lastsc] = false;
  295.     }
  296.  
  297.  
  298. /* sclookup - lookup the number associated with a start condition
  299.  *
  300.  * synopsis
  301.  *    char str[], scnum;
  302.  *    int sclookup;
  303.  *    scnum/0 = sclookup( str );
  304.  */
  305.  
  306. int sclookup( str )
  307. char str[];
  308.  
  309.     {
  310.     return ( findsym( str, sctbl, START_COND_HASH_SIZE )->int_val );
  311.     }
  312.  
  313.  
  314.  
  315.